package com.yitong.equipment.database

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.yitong.equipment.database.dao.UserDao
import com.yitong.equipment.database.entity.ChildBean
import com.yitong.equipment.database.entity.User
import androidx.sqlite.db.SupportSQLiteDatabase

import androidx.room.migration.Migration


@Database(entities = [User::class, ChildBean::class], version = 2, exportSchema = false)
abstract class YtDataBase : RoomDatabase() {

    abstract fun userDao(): UserDao

    companion object {
        @Volatile
        private var instance: YtDataBase? = null
        fun getInstance(context: Context): YtDataBase {
            return instance ?: synchronized(this) {
                instance ?: buildDatabase(context).also { instance = it }
            }
        }

        private fun buildDatabase(context: Context): YtDataBase {
            return Room.databaseBuilder(
                context.applicationContext, YtDataBase::class.java, "yt_equipment" //数据库名称
            ).addMigrations(object : Migration(1, 2) {
                override fun migrate(database: SupportSQLiteDatabase) {
                    database.execSQL(
                        "ALTER TABLE User ADD COLUMN nickname STRING"
                    )
                }
            }).build()
        }

    }

}